package JAVA300.第九章容器;

/**
 * 自写linkedList
 * 添加小的封装，增加泛型
 * 判断索引封装
 */

public class TestLinkedList5<E> {

    private Node first;
    private Node last;
    private int size;

    public void add(E element){
        Node node = new Node(element);

        if (first==null){
            first = node;
            last = node;

        }else {
            node.previous = last;
            node.next = null;

            last.next = node;
            //node = last;
            last = node;

        }
        size++;

    }

    public void insert(int index,E element){
        Node newNode = new Node(element);
        Node node = getNode(index);
        //[a,b,c]

        if (node != null){
            Node up = node.previous;
            Node down = node.next;

            if(index == 0 ){
                node.previous = newNode;
                newNode.next = node;
                first = newNode;

            }else if(index == size+1){

            }else {//核心部分
                up.next = newNode;
                newNode.next = node;
                node.previous = node;
                node.previous = up;

                size++;
            }


        }


    }


    public E get(int index){//从头遍历 效率过慢，提出get1 折半查询


        check_Range(index);
        //[a,b,c,d] 2-c
        Node temp = first;
        for (int i=0;i<index;i++){
            temp = temp.next;
        }

        return (E) temp.element;
    }


    public Object get1(int index){//从头遍历 效率过慢，提出get1 折半查询

        Node temp = getNode(index);
        return temp!=null?(E)temp.element:null;
    }


    public void check_Range(int index){
        if(index < 0 || index > size-1){
            throw new RuntimeException(size+"索引数组不合法");
        }
    }

    public Node getNode(int index){//同get1方法 只是返回值不同
        check_Range(index);

        Node temp = null;

        if(index <= (size>>1)){//在前半段

            //[a,b,c,d] 2-c


            temp = first;
            for (int i=0;i<index;i++){
                temp = temp.next;
            }

        }else {//从后往前
            System.out.println("back");
            temp = last;
            for (int i = size-1;i>index;i--) {
                temp = temp.previous;
            }

        }

        return temp;

    }

    public void remove(int index){
        //首先需要获取node，添加 getNode方法

        Node node = getNode(index);

        if (node!=null){

            if (node.previous == null){
                System.out.println(node.next.element.toString());
               // node.next.previous = null;
                first = node.next;

            }else if (node.next == null){
                System.out.println(node.previous.element.toString());

                last = node.previous;
                //System.out.println(last.next.element.toString());
                //node.previous.next  = null;

            }else {

                node.previous.next = node.next;
                node.next.previous = node.previous;
            }


            size--;

        }



    }

    @Override
    public String toString() {
        //【a,b,c】 first=a,last=c


        StringBuilder sb = new StringBuilder("[");
        Node temp = first;
        //  System.out.print("[");

        while (temp!=null & temp !=last){
            //System.out.print(temp.element+",");
            sb.append(temp.element+",");
            temp = temp.next;

        }
        //System.out.print("]");
        sb.setCharAt(sb.length()-1,']');
        return sb.toString();


    }



    public static void main(String[] args) {
        TestLinkedList5<String> linkedList = new TestLinkedList5<>();
        linkedList.add("a");
        linkedList.add("b");
        linkedList.add("c");
        linkedList.add("d");
        linkedList.add("e");
        linkedList.add("f");


        System.out.println(linkedList);


        //linkedList.insert(0,"hqq");
        linkedList.remove(5);
        System.out.println(linkedList);
    }


}
